package com.iwop.moto.controller.web;

import com.alibaba.fastjson.JSON;
import com.iwop.moto.auth.LoginAuth;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.controller.BaseController;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.BUserService;
import com.iwop.moto.service.UserService;
import com.iwop.moto.utils.stringUtil.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by mobom on 2016/10/14.
 */
@Controller
public class StoreTotalController extends BaseController {

    @Autowired
    UserService userService;

    @Autowired
    BUserService bUserService;

    @RequestMapping("/api/user/total")
    @ResponseBody
    @LoginAuth(isval=true)
    public ResultModel userTotalUserType(HttpServletRequest request) {
        SuperRecordModel superRecordModel=new SuperRecordModel();
        superRecordModel.put("user_type","1");
        List<SuperRecordModel> srm=userService.findUserTotalUserType(superRecordModel);
        superRecordModel.put("user_type","5");
        List<SuperRecordModel> userTotalUserType=userService.findUserTotalUserType(superRecordModel);
        List<SuperRecordModel> bUser=bUserService.getBUserTotalUserType(superRecordModel);
        SuperRecordModel allMap=new SuperRecordModel();
        allMap.put("partner", userTotalUserType);
        allMap.put("repair",srm);
        allMap.put("areas",bUser);
        return getSucceed(allMap, ErrCode.SUCCEED);
    }

    @RequestMapping("/api/user/register/total")
    @ResponseBody
    @LoginAuth(isval=true)
    public ResultModel totalUserType(HttpServletRequest request) {
      try {
          SuperRecordModel superRecordModel=this.getSRModel();
          if(!superRecordModel.getString("dates").equals("month")){
              superRecordModel.put("formatDatesFirst","%Y-%m");
                  superRecordModel.put("dateYearMonth",superRecordModel.getString("ndate"));
              superRecordModel.put("formatMonthDay","%Y-%m-%d");
          }else{
              superRecordModel.put("formatDatesFirst","%Y");
              superRecordModel.put("dateYearMonth",superRecordModel.getString("ndate"));
              superRecordModel.put("formatMonthDay","%Y-%m");
          }
          List<SuperRecordModel> list=userService.getTotalUserType(superRecordModel);
          List<SuperRecordModel> bUser=bUserService.getBUserTotalType(superRecordModel);
          Map map=new HashMap<>();
          map.put("repair",list);
          map.put("user",bUser);
          return getSucceed(map,ErrCode.SUCCEED);
      }catch (Exception e){
            return getErr(ErrCode.ERROR_SYSTEM);
      }
    }
}
